/*
 * Copyright (C) 2013 AdminsProL2
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.l2jserver.gameserver.instancemanager.statistics;

import java.awt.Toolkit;
import java.util.logging.Logger;

import com.l2jserver.Config;
import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.cache.HtmCache;
import com.l2jserver.gameserver.model.L2World;

/**
 * @author fissban
 */

public class Statistics
{
	
	static Logger _log = Logger.getLogger(Statistics.class.getName());
	static final int Reuse = Config.STATISTICS_TIME * 1000;
	
	Statistics()
	{
		if (Config.STATISTICS_GS)
		{
			_log.info("[ Statistics ] Enable");
			ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new StatisticsGS(), Reuse, Reuse);
		}
		else
		{
			_log.info("[ Statistics ] Disable");
		}
		
	}
	
	class StatisticsGS implements Runnable
	{
		@Override
		public void run()
		{
			_log.info("----------------- Estadisticas GameServer ------------");
			_log.info("Usuarios conectados : " + L2World.getInstance().getAllPlayersCount() + " de " + Config.MAXIMUM_ONLINE_USERS);
			long freeMem = ((Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory()) + Runtime.getRuntime().freeMemory()) / 1048576;
			long totalMem = Runtime.getRuntime().maxMemory() / 1048576;
			_log.info("Gameserver free memory " + freeMem + " Mb de " + totalMem + " Mb");
			Toolkit.getDefaultToolkit().beep();
			_log.info("Cache[HTML]: " + String.format("%.3f", HtmCache.getInstance().getMemoryUsage()) + " megabytes");
			_log.info("----------------- Estadisticas GameServer ------------");
		}
	}
	
	public static Statistics getInstance()
	{
		return SingletonHolder._instance;
	}
	
	static class SingletonHolder
	{
		protected static final Statistics _instance = new Statistics();
	}
	
}